﻿/**********************************************************************
* Description:  Concrete implementation of ICachable that utilizes the current HTTP Session
* Created By:   Jason Offutt @ Central Christian Church of the East Valley
* Date Created: 10/21/2009
*
* $Workfile: SessionCache.cs $
* $Revision: 1 $
* $Header: /trunk/Arena.Custom.Cccev/Arena.Custom.Cccev.FrameworkUtils/BLL/SessionCache.cs   1   2009-10-22 09:57:01-07:00   JasonO $
*
* $Log: /trunk/Arena.Custom.Cccev/Arena.Custom.Cccev.FrameworkUtils/BLL/SessionCache.cs $
*  
*  Revision: 1   Date: 2009-10-22 16:57:01Z   User: JasonO 
**********************************************************************/

using System;
using System.Web;
using System.Web.SessionState;
using Arena.Custom.Cccev.FrameworkUtils.Interfaces;

namespace Arena.Custom.Cccev.FrameworkUtils.BLL
{
    public sealed class SessionCache : ICachable
    {
        private static SessionCache instance;

        /// <summary>
        /// Singleton implementation to limit how Cache is accessed.
        /// </summary>
        public static SessionCache Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new SessionCache();
                }

                return instance;
            }
        }

        public object Cache
        {
            get
            {
                if (HttpContext.Current == null)
                {
                    throw new NullReferenceException("If using this functionality outside of ASP.NET, set the CacheType of the caller to CacheType.Dictionary.");
                }

                return HttpContext.Current.Session;
            }
        }

        public int Count
        {
            get { return ((HttpSessionState)Cache).Count; }
        }

        public object Get(string key)
        {
            return ((HttpSessionState) Cache)[key];
        }

        public void Insert(string key, object value)
        {
            ((HttpSessionState) Cache).Add(key, value);
        }

        public void Remove(string key)
        {
            ((HttpSessionState) Cache).Remove(key);
        }

        private SessionCache()
        {
        }
    }
}
